﻿using System.IO;
using System.Net;
using System.Text;

namespace ExpressesClass.SfApi.Util
{
    internal class HttpClientUtil
    {
        public string postSFAPI(string url, string xml, string verifyCode)
        {
            string result = "";
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat("{0}={1}", "xml", xml);
            stringBuilder.Append("&");
            stringBuilder.AppendFormat("{0}={1}", "verifyCode", verifyCode);
            byte[] bytes = Encoding.UTF8.GetBytes(stringBuilder.ToString());
            httpWebRequest.ContentLength = (long)bytes.Length;
            using (Stream requestStream = httpWebRequest.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
            }
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream responseStream = httpWebResponse.GetResponseStream();
            using (StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8))
            {
                result = streamReader.ReadToEnd();
            }
            return result;
        }
    }
}
